package com.egg.ylt.Utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.egg.ylt.R;
import com.yonyou.framework.library.common.utils.ScreenUtil;
import com.yonyou.framework.library.dialog.AlertDialog;

/**
 * Created by sichaoqi on 2018/8/29.
 */

public class CustomUtils {

    /**
     * 设置textView中间线
     */
    public static TextView setMiddleLine(TextView tv) {
        tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        return tv;
    }

    /**
     * 取消textView中间线
     */
    public static TextView cancelMiddleLine(TextView tv) {
        tv.getPaint().setFlags(0);
        tv.invalidate();
        return tv;
    }

    /**
     * 动态设置沉浸式状态栏的高度
     */
    public static void setImmersiveStatusBar(Context context, View view) {
        if (StatusBarUtil.isMi()) {
            return;
        }

        //动态设置沉浸式状态栏的高度
        int statusBarHeight = ScreenUtil.getStatusBarHeight(context);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.topMargin += statusBarHeight;
        view.setLayoutParams(layoutParams);
    }

    /**
     * 隐藏手机中间4位数字
     */
    public static String setPhoneHide(String phone) {
        return phone.substring(0, 3) + "****" + phone.substring(7, 11);
    }


    /**
     * 拨打电话
     *
     * @param context
     * @param phone
     */
    public static void setCallPhoneDialog(final Context context, final String phone) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context)
                .setContentView(R.layout.dialog_alert_make_phone_call)
                .setWidthAndHeight(Constants.SCREEN_WIDTH - 150, -2)
                .setText(R.id.tv_phone, phone)
                .create();
        alertDialog.setOnClickListener(R.id.tv_cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.setOnClickListener(R.id.tv_confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialPhone(context, phone);
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    private static void dialPhone(Context context, String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        context.startActivity(intent);
    }
}
